package com.intellipark.androidautonomouscar;


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.os.AsyncTask;
import android.util.Log;


public class GetSensorValues extends AsyncTask<Void, Void, Void> {

	boolean success = false;
	String url;
	double threshold = 0.3;
	double readout = 0;
	MainActivity m;
	public GetSensorValues(String url, MainActivity m) {
		this.url = url;
		this.m = m;

	}

	@Override
	protected Void doInBackground(Void... params) {
		
		System.out.println("URL being used: " + url);
		final HttpGet getRequest = new HttpGet(url);

		try
		{
			HttpParams httpParameters = new BasicHttpParams();

			// Set the timeout in milliseconds until a connection is
			// established.
			// The default value is zero, that means the timeout is not used.
			int timeoutConnection = 3000;
			HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

			// Set the default socket timeout (SO_TIMEOUT)
			// in milliseconds which is the timeout for waiting for data.
			int timeoutSocket = 5000;
			HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

			DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
			final HttpResponse getResponse = httpClient.execute(getRequest);
			final int statusCode = getResponse.getStatusLine().getStatusCode();

			if (statusCode != HttpStatus.SC_OK)
			{
				Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url);
				success = false;
				return null;
			}

			final HttpEntity getResponseEntity = getResponse.getEntity();
			final InputStream httpResponseStream = getResponseEntity.getContent();
			//final Reader inputStreamReader = new InputStreamReader(httpResponseStream);
			String out = convertStreamToString(httpResponseStream);
			//System.out.println(out);
			readout = Double.parseDouble(out);
			

			success = true;
		}
		catch (final Exception e)
		{
			getRequest.abort();
			e.printStackTrace();
			Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
			success = false;
		}

		return null;
	}

	
	static String convertStreamToString(java.io.InputStream is) {
	    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
	    return s.hasNext() ? s.next() : "";
	}
	
	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if(success){
		if(readout >= threshold){
			m.spot1 = false;
			m.spotTextView.setText("2");
		}
		else{
			m.spot1 = true;
			m.spotTextView.setText("1");
		}
		
			//m.trigger = true;
		}
		
	}

	@Override
	protected void onPreExecute() {
		//Loading is used in other places in the app to prevent the user from making
		//unnecessary requests

	}

}